package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    // 定义切入点
    @Pointcut("execution(* com.sky.mapper.*.*(..) ) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {}


    @Pointcut("execution(* com.sky.controller.*.*.*(..))")
    public void getAddressInfoPointCut() {}
    // 定义前置通知
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行数据填充...");
        // 获取方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

        // 获取方法的注解
        AutoFill annotation = methodSignature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = annotation.value();
        // 获取所有参数
        Object[] args = joinPoint.getArgs();
        if (args==null || args.length == 0)
            return;
        LocalDateTime localDateTime = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        Object arg = args[0];
        switch (operationType) {
            case INSERT:
                try {
                    Method setCreateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                    Method setCreateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                    Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                    Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                    setCreateTime.invoke(arg, localDateTime);
                    setCreateUser.invoke(arg, currentId);
                    setUpdateTime.invoke(arg, localDateTime);
                    setUpdateUser.invoke(arg, currentId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case UPDATE:
                try {
                    Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                    Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                    setUpdateTime.invoke(arg, localDateTime);
                    setUpdateUser.invoke(arg, currentId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Before("getAddressInfoPointCut()")
    public void getMethod(JoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        // 获取请求路径、请求方法、请求参数,请求方法名
        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String params = Arrays.toString(joinPoint.getArgs());
        String methodName = joinPoint.getSignature().getName();
        log.info("请求路径: {}, 请求方法: {}, 请求参数: {}, 请求方法名: {}", url, method, params,methodName);
        // ...原有填充逻辑
    }
}
